package com.itheima.sh.test;

import com.itheima.sh.dao.OrderMapper;
import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;

public class MybatisTest02 {
    private static OrderMapper mapper = null;

    @BeforeClass
    public static void beforeClass() throws Exception {
        //1.创建工厂创造类对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //2.获取工厂对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = builder.build(is);
        //3.创建会话对象
        SqlSession sqlSession = sessionFactory.openSession(true);
        //4.获取接口的动态代理对象
        mapper = sqlSession.getMapper(OrderMapper.class);
    }
    /*
         //需求：通过订单编号20140921003查询出**订单信息**，并查询出**下单人信息**。
        Order queryOneToOne(@Param("orderNumber") String orderNumber);
     */
    @Test
    public void queryOneToOne(){
        Order order = mapper.queryOneToOne("20140921003");
        System.out.println("order = " + order);
    }
    /*
        【需求】：查询订单号为20140921001的订单的详情信息即查询订单信息+订单中的商品信息；
         Order queryManyToMany(@Param("orderNumber") String orderNumber);
     */
    @Test
    public void queryManyToMany(){
        Order order = mapper.queryManyToMany("20140921001");
        System.out.println("order = " + order);
    }
    /*
          【需求】根据订单号（20140921001）
​		**查询订单信息**
​		**查询订单所属用户信息**
​		**查询订单中的详细商品信息**
     Order queryManyToMany2(@Param("orderNumber") String orderNumber);
     */
    @Test
    public void queryManyToMany2(){
        Order order = mapper.queryManyToMany2("20140921001");
        System.out.println("order = " + order);
    }

    /*
        延迟加载：
        延迟加载需求：通过订单编号20140921003查询order并延迟加载user。就是演示上述演示过的一对一的表关系案例。
        Order queryLazyLoading(@Param("orderNumber") String orderNumber);
     */
    @Test
    public void queryLazyLoading(){
        Order order = mapper.queryLazyLoading("20140921003");
        System.out.println("order = " + order);
    }
}
